package work.xiaohong.gupiao.time;

import com.alibaba.fastjson.JSON;
import work.xiaohong.gupiao.pojo.HttpResult;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.List;
import java.util.Map;

public class HttpUtil {
 
    public static HttpResult sendGetReq(String url, List<NameValuePair> nameValuePairList, String encoding, String cookie, String authorization) throws Exception {
        String body = "";
 
        //创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
 
        /**
         * 创建URIBuilder
         */
        URIBuilder uriBuilder = new URIBuilder(url);
 
        if (nameValuePairList != null){
            /**
             * 设置参数
             */
            uriBuilder.addParameters(nameValuePairList);
        }
 
        HttpGet httpGet = new HttpGet(uriBuilder.build());
        //设置header信息
        httpGet.setHeader ("Accept", "*/*");
        httpGet.setHeader ("Accept-Encoding", "gzip, deflate");
        httpGet.setHeader ("Accept-Language", "zh-CN,zh;q=0.9");
        httpGet.setHeader ("Host", "api.money.126.net");
        httpGet.setHeader ("Referer", "http://quotes.money.163.com/");
        httpGet.setHeader ("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36");
//        httpGet.setHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
 
        if (StringUtils.isNotBlank(cookie)){
            httpGet.setHeader("Cookie", cookie);
        }

        if (StringUtils.isNotBlank(authorization)){
            httpGet.setHeader("Authorization", authorization);
        }
 
        //执行请求操作，并拿到结果（同步阻塞）
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //获取结果实体
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            //按指定编码转换结果实体为String类型
            body = EntityUtils.toString(entity, encoding);
        }
        //释放链接
        response.close();


        String result1 = body.substring(body.indexOf("(")+1,body.indexOf(")"));
        Map<String, HttpResult> map = JSON.parseObject(result1, Map.class);

        HttpResult httpResult = new HttpResult();

        for (String s : map.keySet()) {
//            System.out.println(s+"  :  " + map.get(s));
            HttpResult hr = JSON.parseObject(String.valueOf(map.get(s)), HttpResult.class);
            httpResult =  hr;
        }

        return httpResult;

    }
 
    public static void main(String[] args) {
        String url = "http://api.money.126.net/data/feed/004753,money.api?callback=_ntes_quote_callback123";
 
//        List<NameValuePair> reqParams = new ArrayList<>();
//        reqParams.add(new BasicNameValuePair("userName","发送get请求"));
//        reqParams.add(new BasicNameValuePair("password","123456"));
 
        try {
            HttpResult httpResult = sendGetReq (url, null, "utf-8",null,null);
//            System.out.println(httpResult);
        } catch (Exception e) {
            e.printStackTrace ( );
        }
    }
 
}